package dao

import (
	"TTMS/model"
	"TTMS/utils"
	"time"
)

// AddSeat 添加座位
func AddSeat(seat *model.Seat) error {
	//写 sql 语句
	sqlStr := "insert into seats (col_id, row_id, state, row_num, hall_id, schedule_id) values (?,?,?,?,?,?)"
	//执行
	_, err := utils.DB.Exec(sqlStr, seat.Col, seat.Row, seat.State, seat.RowNum, seat.HallID, seat.ScheduleID)
	if err != nil {
		return err
	}
	return nil
}

// AddSeats 添加座位
func AddSeats(hall *model.Hall, scheduleID int64) error {
	var i, j int64
	for i = 1; i <= hall.ColNum; i++ {
		for j = 1; j <= hall.RowNum; j++ {
			seat := &model.Seat{
				Col:        i,
				Row:        j,
				State:      0,
				HallID:     hall.ID,
				ScheduleID: scheduleID,
				RowNum:     hall.RowNum,
			}
			err := AddSeat(seat)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

// DeleteSeats 根据演出计划的删除座位
func DeleteSeats(scheduleID int64) error {
	//写 sql 语句
	sqlStr := "delete from seats where schedule_id=?"
	//执行
	_, err := utils.DB.Exec(sqlStr, scheduleID)
	if err != nil {
		return err
	}
	return nil
}

// GetSeats 根据演出计划ID获取座位
func GetSeats(scheduleId int64) ([]*model.Seat, error) {
	//写 sql 语句
	sqlStr := "select id, col_id, row_id, state, hall_id,schedule_id, row_num,order_id from seats where schedule_id=?"
	//执行
	rows, err := utils.DB.Query(sqlStr, scheduleId)
	if err != nil {
		return nil, err
	}
	var seats []*model.Seat
	for rows.Next() {
		seat := &model.Seat{}
		rows.Scan(&seat.ID, &seat.Col, &seat.Row, &seat.State, &seat.HallID, &seat.ScheduleID, &seat.RowNum, &seat.OrderID)
		seats = append(seats, seat)
	}
	return seats, nil
}

// GetSeatByID 根据座位ID获取座位
func GetSeatByID(seatID int64) *model.Seat {
	//写 sql 语句
	sqlStr := "select id, col_id, row_id, state, hall_id,schedule_id, row_num, order_id from seats where id=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, seatID)
	seat := &model.Seat{}
	row.Scan(&seat.ID, &seat.Col, &seat.Row, &seat.State, &seat.HallID, &seat.ScheduleID, &seat.RowNum, &seat.OrderID)
	return seat
}

// UpdateSeatByID 根据座位ID修改座位状态
func UpdateSeatByID(id int64, orderID string, state int64) error {
	//写 sql 语句
	sqlStr := "update seats set state=?, order_id=? where id=?"
	//执行
	_, err := utils.DB.Exec(sqlStr, state, orderID, id)
	if err != nil {
		return err
	}
	return nil
}

// UpdateSeatByStateIS2 根据座位状态，修改座位状态
func UpdateSeatByStateIS2(id int64) error {
	time.Sleep(30 * time.Second)
	//写 sql 语句
	sqlStr := "update seats set state=? where id=? and state=2"
	//执行
	_, err := utils.DB.Exec(sqlStr, 0, id)
	if err != nil {
		return err
	}
	return nil
}

// GetSeatsByOrderID 根据订单号ID获取座位
func GetSeatsByOrderID(orderID string) ([]*model.Seat, error) {
	//写 sql 语句
	sqlStr := "select id, col_id, row_id, state, hall_id,schedule_id, row_num, order_id from seats where order_id=?"
	//执行
	rows, err := utils.DB.Query(sqlStr, orderID)
	if err != nil {
		return nil, err
	}
	var seats []*model.Seat
	for rows.Next() {
		seat := &model.Seat{}
		rows.Scan(&seat.ID, &seat.Col, &seat.Row, &seat.State, &seat.HallID, &seat.ScheduleID, &seat.RowNum, &seat.OrderID)
		seats = append(seats, seat)
	}
	return seats, nil
}
